﻿// XmlVisualizer
// Copyright (C) 2009  Roman Betík - rbetik@gmail.com

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Configuration;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Xml.XPath;
using System.Collections;
using System.Collections.ObjectModel;
using System.Diagnostics;

using XmlVisualizer.Models;
using XmlVisualizer.Controllers;
using XmlVisualizer.DiagramDesigner;
using XmlVisualizer.DiagramDesigner.Controls;
using System.Globalization;
using Tomers.WPF.Localization;

namespace XmlVisualizer {
    public partial class Document : Window {
        /// <summary>
        /// Invokes the OpenFileDialog, user selects the file to open and application loads the file.
        /// </summary>
        public void OpenFile() {
			appModel.DocumentClosed += new Action<object, EventArgs>(appModel_DocumentClosed);
            // opens the OpenFileFialog, gets a filename and sends this information to the controller
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Multiselect = false;

			ofd.Filter = "Xmv files (*.xmv)|*.xmv|Xml files (*.xml)|*.xml;|Xsd files (*.xsd)|*.xsd|All Files|*.*";

            string filePath;
            string safeFilePath;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                filePath = ofd.FileName;
                safeFilePath = ofd.SafeFileName;
				
                // if this document is empty, load the file here
                if (empty) {
                    FileName = filePath;
					Load();
					ShowDocument();
					empty = false;
                } else { // otherwise start new process
					//Document doc = new Document(filePath);
					//doc.ShowDocument();
					//SingleInstanceApplicationWrapper.NewWindow(filePath);
					ProcessStartInfo psi = new ProcessStartInfo(Process.GetCurrentProcess().MainModule.FileName);
					psi.Arguments = filePath;
					System.Diagnostics.Process.Start(psi);
                }
            }
        }

		/// <summary>
		/// Invokes SaveFileDialog.
		/// </summary>
		public void SaveFileDialog() {
			System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			
			if (appModel.Mode == Mode.Xml) {
				saveFileDialog.Filter = "Xmv files (*.xmv)|*.xmv|Xml files (*.xml)|*.xml|All files (*.*)|*.*";
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.RestoreDirectory = true;
			} else if (appModel.Mode == Mode.Xsd) {
				saveFileDialog.Filter = "Xsd files (*.xsd)|*.xsd|All files (*.*)|*.*";
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.RestoreDirectory = true;
			}

			if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
				string filePath = saveFileDialog.FileName;
				controller.SaveDocument(filePath);
			}
		}
    }
}